package handlers

import (
	"github.com/gin-gonic/gin"
	"live-stream-pro/models"
	"live-stream-pro/pkg"
	"strconv"
)

func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名和密码不能为空",
		})
		return
	}
	user := models.User{
		Username: username,
		Password: pkg.Md5Str(password),
	}
	err := models.DB.Create(&user).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "注册成功",
		})
		return
	}
}

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名密码不能为空",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username = ?", username).First(&user).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在不允许登录",
		})
		return
	}
	if pkg.Md5Str(password) != user.Password {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	user = models.User{
		Username: username,
		Password: password,
	}
	err = models.DB.Find(&user).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "登录成功",
		})
	}
}

func Add(c *gin.Context) {
	title := c.PostForm("title")
	author := c.PostForm("author")
	img := c.PostForm("img")
	likecount, _ := strconv.Atoi(c.PostForm("like_count"))
	focuscount, _ := strconv.Atoi(c.PostForm("focus_count"))
	userId, _ := strconv.Atoi(c.PostForm("user_id"))
	live := models.Live{
		Title:      title,
		Author:     author,
		Img:        img,
		LikeCount:  likecount,
		FocusCount: focuscount,
		UserId:     userId,
	}
	err := models.DB.Create(&live).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "发布成功",
		})
		return
	}
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("img")
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "图片获取失败",
		})
		return
	}
	if file.Size > 2*1024*1024 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "图片大小不能超过2M",
		})
		return
	}
	path := "/static/img" + file.Filename
	err = c.SaveUploadedFile(file, path)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "上传失败",
		})
		return
	} else {
		url := "http://" + c.Request.Host + file.Filename
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "上传成功",
			"data": url,
		})
		return
	}
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	office := (page - 1) * size
	query := models.DB.Model(&models.Live{})

	var count int64
	query.Count(&count)

	var live []models.Live
	err := query.Offset(office).Limit(size).Order("like_count").Preload("User").Find(&live).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "查询成功",
			"data": gin.H{
				"count": count,
				"live":  live,
			},
		})
		return
	}
}
